package com.zfl.practice;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class AtomicDemo02 {
    public static void main(String[] args) {
        ExecutorService pool = Executors.newCachedThreadPool();
        CountDownLatch count = new CountDownLatch(50);
        BankAccount account = new BankAccount();
        for (int i = 0; i < 50; i++) {
            pool.execute(()->{
                for (int j = 0; j < 1000; j++) {
//                    account.addNum();
                    account.addByAtomic();
                }
                count.countDown();
            });
        }
        pool.shutdown();
        try {
            count.await();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        System.out.println("account.getNumber() = " + account.getNumber());
    }
}
